﻿using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.Entity.Department;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<Common.View.GoodsBarcodeView> GetGoodsBarcodeViews(List<int> ids)
        {
            var items = new List<Common.View.GoodsBarcodeView>();
            using(var context = new Data.Common.Context.SharpErpDbContext())
            {
                var eightClassItems = context.SelectAll(context.ItemNineClasses, p => ids.Contains(p.Id));
                var parentIds = eightClassItems.Select(p => p.ParentId);
                var sevenClassItems = context.SelectAll(context.ItemEightClasses, p => parentIds.Contains(p.Id));
                foreach(var item in eightClassItems)
                {
                    var sevenItem = sevenClassItems.First(p => p.Id == item.ParentId);
                    var parentNames = sevenItem.ParentDesc?.Split(new string[] {"$$"}, StringSplitOptions.RemoveEmptyEntries);
                    if (parentNames == null) parentNames = new string[0];
                    items.Add(new Common.View.GoodsBarcodeView {
                        Code = sevenItem.Code,
                        Price = item.SalePrice,
                        Unit = sevenItem.Unit,
                        Name = parentNames.Length > 0 ? parentNames[1] : "",
                        Product = parentNames.Length > 1 ? parentNames[2] : "",
                        Width = parentNames.Length > 2 ? parentNames[3] : "",
                        Face = parentNames.Length > 3 ? parentNames[4] : "",
                        Standard = sevenItem.Standard,
                        EightClassId = item.Id,
                    });
                }
            }
            return items;
        }

        public List<Common.View.GoodsBarcodeView> GetProductionBarcodeViews(List<int> ids)
        {
            var items = new List<Common.View.GoodsBarcodeView>();
            using (var context = new Data.Common.Context.SharpErpDbContext())
            {
                var tenClassItems = context.SelectAll(context.ProductionTenClasses, p => ids.Contains(p.Id));
                var parentIds = tenClassItems.Select(p => p.ParentId);
                if (tenClassItems == null || tenClassItems.Count <= 0) return items;
                var nineClassItems = context.SelectAll(context.ProductionNineClasses, p => parentIds.Contains(p.Id));
                if (nineClassItems == null || nineClassItems.Count <= 0) return items;
                parentIds = nineClassItems.Select(p => p.ParentId);
                var eightClassItems = context.SelectAll(context.ProductionEightClasses, p => parentIds.Contains(p.Id));
                if (eightClassItems == null || eightClassItems.Count <= 0) return items;
                foreach (var item in tenClassItems)
                {
                    var nineItem = nineClassItems.First(p => p.Id == item.ParentId);
                    var eightItem = eightClassItems.First(p => p.Id == nineItem.ParentId);
                    var parentNames = nineItem.ParentDesc?.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries);
                    if (parentNames == null) parentNames = new string[0];
                    items.Add(new Common.View.GoodsBarcodeView
                    {
                        Code = eightItem.Code,
                        Price = item.SalePrice,
                        Unit = nineItem.Unit,
                        Name = parentNames.Length > 0 ? parentNames[1] : "",
                        Product = parentNames.Length > 1 ? parentNames[2] : "",
                        Width = parentNames.Length > 2 ? parentNames[3] : "",
                        Face = parentNames.Length > 3 ? parentNames[4] : "",
                        Standard = nineItem.Standard,
                        EightClassId = item.Id,
                    });
                }
            }
            return items;
        }

        public List<Common.View.GoodsBarcodeView> GetMaterialBarcodeViews(List<int> ids)
        {
            var items = new List<Common.View.GoodsBarcodeView>();
            using (var context = new Data.Common.Context.SharpErpDbContext())
            {
                var nineClassItems = context.SelectAll(context.MaterialNineClasses, p => ids.Contains(p.Id));
                var parentIds = nineClassItems.Select(p => p.ParentId);
                var eightClassItems = context.SelectAll(context.MaterialEightClasses, p => parentIds.Contains(p.Id));
                foreach (var item in nineClassItems)
                {
                    var sevenItem = eightClassItems.First(p => p.Id == item.ParentId);
                    var parentNames = sevenItem.ParentDesc?.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries);
                    if (parentNames == null) parentNames = new string[0];
                    items.Add(new Common.View.GoodsBarcodeView
                    {
                        Code = sevenItem.Code,
                        Price = item.BuyPrice,
                        Unit = sevenItem.Unit,
                        Name = parentNames.Length > 0 ? parentNames[1] : "",
                        Product = parentNames.Length > 1 ? parentNames[2] : "",
                        Width = parentNames.Length > 2 ? parentNames[3] : "",
                        Face = parentNames.Length > 3 ? parentNames[4] : "",
                        EightClassId = item.Id,
                    });
                }
            }
            return items;
        }


        public List<Common.View.OtherBasicDataView> GetOtherBasicDataView(string kvKey, List<int> ids)
        {
            var result = new List<Common.View.OtherBasicDataView>();
            switch (kvKey)
            {
                case Common.Entity.KVEntity.KEY_FEETYPE_SECOND_CLASS:
                    {
                        var data = GetAllKvItemClass<Common.Entity.FeeTypeSecondClass>(kvKey);
                        data = data.Where(p => ids != null && ids.Contains(p.Id)).ToList<FeeTypeSecondClass>();
                        if (data != null && data.Count > 0)
                        {
                            var firstIds = data.Select(p => p.ParentId).ToList<int>();
                            var firstClassList = GetAllKvItemClass<FeeTypeFirstClass>(KVEntity.KEY_FEETYPE_FIRST_CLASS).Where(p => firstIds.Contains(p.Id)).ToList<FeeTypeFirstClass>();
                            var id2Name = new Dictionary<int, string>();
                            foreach (var item in firstClassList)
                            {
                                id2Name.Add(item.Id, item.Name);
                            }
                            foreach (var item in data)
                            {
                                result.Add(new Common.View.OtherBasicDataView()
                                {
                                    Id = item.Id,
                                    Name = item.Name,
                                    ParentId = item.ParentId,
                                    ParentName = id2Name[item.ParentId],
                                    Alias = item.Alias,
                                    Code = item.Code,
                                    Note = item.Note,
                                    PinYin = item.PinYin
                                });
                            }
                        }
                    }
                    break;
                case Common.Entity.KVEntity.KEY_INVOICE_SECOND_CLASS:
                    {
                        var data = GetAllKvItemClass<Common.Entity.InvoiceSecondClass>(kvKey);
                        data = data.Where(p => ids != null && ids.Contains(p.Id)).ToList<InvoiceSecondClass>();
                        if (data != null && data.Count > 0)
                        {
                            var firstIds = data.Select(p => p.ParentId).ToList<int>();
                            var firstClassList = GetAllKvItemClass<InvoiceFirstClass>(KVEntity.KEY_INVOICE_FIRST_CLASS).Where(p => firstIds.Contains(p.Id)).ToList<InvoiceFirstClass>();
                            var id2Name = new Dictionary<int, string>();
                            foreach (var item in firstClassList)
                            {
                                id2Name.Add(item.Id, item.Name);
                            }
                            foreach (var item in data)
                            {
                                result.Add(new Common.View.OtherBasicDataView()
                                {
                                    Id = item.Id,
                                    Name = item.Name,
                                    ParentId = item.ParentId,
                                    ParentName = id2Name[item.ParentId],
                                    Alias = item.Tax,
                                    Code = item.Code,
                                    Note = item.Note,
                                    PinYin = item.PinYin
                                });
                            }
                        }
                    }
                    break;
                case Common.Entity.KVEntity.KEY_CASHBANK_SECOND_CLASS:
                    {
                        var data = GetAllKvItemClass<Common.Entity.CashBankSecondClass>(kvKey);
                        data = data.Where(p => ids != null && ids.Contains(p.Id)).ToList<CashBankSecondClass>();
                        if (data != null && data.Count > 0)
                        {
                            var firstIds = data.Select(p => p.ParentId).ToList<int>();
                            var firstClassList = GetAllKvItemClass<CashBankFirstClass>(KVEntity.KEY_CASHBANK_FIRST_CLASS).Where(p => firstIds.Contains(p.Id)).ToList<CashBankFirstClass>();
                            var id2Name = new Dictionary<int, string>();
                            foreach (var item in firstClassList)
                            {
                                id2Name.Add(item.Id, item.Name);
                            }
                            foreach (var item in data)
                            {
                                result.Add(new Common.View.OtherBasicDataView()
                                {
                                    Id = item.Id,
                                    Name = item.Name,
                                    ParentId = item.ParentId,
                                    ParentName = id2Name[item.ParentId],
                                    Alias = item.Account,
                                    Code = item.Code,
                                    Note = item.Note,
                                    PinYin = item.Bank,
                                });
                            }
                        }
                    }
                    break;
                case Common.Entity.KVEntity.KEY_OTHERINCOME_SECOND_CLASS:
                    {
                        var data = GetAllKvItemClass<Common.Entity.OtherIncomeSecondClass>(kvKey);
                        data = data.Where(p => ids != null && ids.Contains(p.Id)).ToList<OtherIncomeSecondClass>();
                        if (data != null && data.Count > 0)
                        {
                            var firstIds = data.Select(p => p.ParentId).ToList<int>();
                            var firstClassList = GetAllKvItemClass<OtherIncomeFirstClass>(KVEntity.KEY_OTHERINCOME_FIRST_CLASS).Where(p => firstIds.Contains(p.Id)).ToList<OtherIncomeFirstClass>();
                            var id2Name = new Dictionary<int, string>();
                            foreach (var item in firstClassList)
                            {
                                id2Name.Add(item.Id, item.Name);
                            }
                            foreach (var item in data)
                            {
                                result.Add(new Common.View.OtherBasicDataView()
                                {
                                    Id = item.Id,
                                    Name = item.Name,
                                    ParentId = item.ParentId,
                                    ParentName = id2Name[item.ParentId],
                                    Alias = item.Tax,
                                    Code = item.Code,
                                    Note = item.Note,
                                    PinYin = item.PinYin
                                });
                            }
                        }
                    }
                    break;
            }
            return result;
        }
        public List<Common.View.CompanyStaffView> GetCompanyStaffView(List<int> ids)
        {
            var result = new List<Common.View.CompanyStaffView>();
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var staffs = new List<CompanyStaff>(context.SelectAll(context.CompanyStaffs,
                    p => ids != null && ids.Contains(p.Id) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
                if(staffs != null && staffs.Count > 0)
                {
                    foreach(var item in staffs)
                    {
                        var staffView = new Common.View.CompanyStaffView();
                        SharpErp.Common.Util.BeanUtils.CopyTo<Common.View.CompanyStaffView>(item, staffView);
                        staffView.Department = GetAllKvItemClass<CompanyDepartment>(KVEntity.KEY_COMPANY_DEPARTMENT).First(p => p.Id == item.ParentId).Name;
                        result.Add(staffView);
                    }
                }
            }
            return result;
        }
    }
}
